<?php
include '_mysql.php';
include '_getuserID.php';

if($userID != null)
{
	$pageData = explode("-", $_POST['id']);
	$rate = mysql_real_escape_string($_POST['rate']);
	if(isset($_POST['rate']) && isset($_POST['id']))
	{
		if(($rate == "up" || $rate == "down") && is_numeric($pageData[1]))
		{
			$query = mysql_query("SELECT * FROM comments_rate_log WHERE user_ID=$userID AND comment_ID=".$pageData[1]);
			if(mysql_num_rows($query) == 0) {
				$query2 = mysql_query("INSERT INTO comments_rate_log (parent_ID, comment_ID, user_ID, rate) VALUES (".$pageData[0].", ".$pageData[1].", $userID, '$rate')");
			} else {
				$query2 = mysql_query("UPDATE comments_rate_log SET rate='$rate' WHERE user_ID=$userID AND comment_ID=$pageData[1]");
			}
			if($query2) {
				$uvote = mysql_num_rows(mysql_query("SELECT rate FROM comments_rate_log WHERE comment_ID=".$pageData[1]." AND rate='up'"));
				$dvote = mysql_num_rows(mysql_query("SELECT rate FROM comments_rate_log WHERE comment_ID=".$pageData[1]." AND rate='down'"));
				echo ($uvote-$dvote);
			}
			else {
				echo $rate." : ".mysql_error();
				mysql_close();	
			}
		}
	}
}
mysql_close();
?>